package ru.tomsk.autovokzal.activity;

import java.util.ArrayList;

import ru.tomsk.autovokzal.R;
import ru.tomsk.autovokzal.activity.adapter.TimeTableAdapter;
import ru.tomsk.autovokzal.tools.DataBaseTools;
import ru.tomsk.autovokzal.tools.TimeTableItem;
import android.app.Activity;
import android.os.Bundle;
import android.app.*;
import android.widget.*;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.*;

public class TimetableActivity extends Activity {

	private GridView find_result_list_time_table;
	private EditText sity_autocomplete_text;
	private String tag = TimetableActivity.class.getCanonicalName();
	private TimeTableAdapter adapter;
	private TextWatcher watcher;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.time_table);
		this.find_result_list_time_table = (GridView) findViewById(R.id.find_result_list_time_table);
		this.sity_autocomplete_text = (EditText) findViewById(R.id.sity_autocomplete_text);

		DataBaseTools dbt = new DataBaseTools(getApplicationContext());
		ArrayList<TimeTableItem> fillTimeTableArray = dbt.fillTimeTableArray();
		adapter = new TimeTableAdapter(getApplicationContext(),
				fillTimeTableArray);
		watcher = new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				adapter.getFilter().filter(s);

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			public void afterTextChanged(Editable s) {

			}
		};
		sity_autocomplete_text.addTextChangedListener(watcher);
		find_result_list_time_table.setAdapter(adapter);
		Log.i(tag, fillTimeTableArray.size() + "");
	}

	@Override
	protected void onResume() {
		adapter.notifyDataSetChanged();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		sity_autocomplete_text.removeTextChangedListener(watcher);
		super.onDestroy();
	}
}
